package ru.spbsu.neural;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;

public class PlusDigitizer implements Digitizer<Integer> {
	
	private static final Integer HEIGHT = 10;
	private static final Integer WIDTH = 10;
	private static final Integer BLACK = 0;
	private static final Integer WHITE = 255;

	@Override
	public List<Integer> digitize(File file) {
		List<Integer> result = new ArrayList<Integer>();
		BufferedImage image;

		try {
			image = ImageIO.read(file);

			for (int x = 0; x < WIDTH; ++x) {
				for (int y = 0; y < HEIGHT; ++y) {
					int clr = image.getRGB(x, y);
					int red = (clr & 0x00ff0000) >> 16;
					int green = (clr & 0x0000ff00) >> 8;
					int blue = clr & 0x000000ff;

					if (red == green && green == blue && blue == WHITE) {
						result.add(0);
					} else if (red == green && green == blue && blue == BLACK) {
						result.add(1);
					} else {
						return Collections.emptyList();
					}

				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}
	
}
